package com.dinadi.yidi.ui.activity;

import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.MainPagerAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.ui.fragment.ArticleFragment;
import com.dinadi.yidi.ui.fragment.ExhibitionMatchFragment;
import com.dinadi.yidi.ui.fragment.SDesignerFragment;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/5/15   搜索看点
 * Time: 15:28
 */

public class SearchExhibitionActivity extends BaseActivity {
    private View view;
    private TextView cancel;
    private ImageView clear_phone;
    private EditText edit_search;
    private ViewPager id_activity_main_vp;
    private RadioButton rb_article, rb_exhibition_match;
    private MainPagerAdapter mAdapter;
    private List<BaseFragment> fragments = new ArrayList<>();
    private List<RadioButton> radioButtons = new ArrayList<>();


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(SearchExhibitionActivity.this).inflate(R.layout.activity_search_exhibition, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        id_activity_main_vp = (ViewPager) view.findViewById(R.id.id_activity_main_vp);
        rb_article = (RadioButton) view.findViewById(R.id.rb_article);
        rb_exhibition_match = (RadioButton) view.findViewById(R.id.rb_exhibition_match);
        cancel = (TextView) view.findViewById(R.id.cancel);
        clear_phone = (ImageView) view.findViewById(R.id.clear_phone);
        edit_search = (EditText) view.findViewById(R.id.edit_search);
        edit_search.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0)
                    clear_phone.setVisibility(View.GONE);
                else
                    clear_phone.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initListener() {
        registerClickListener(cancel, clear_phone);
        edit_search.setOnEditorActionListener(editorActionListener);
        rb_article.setOnClickListener(this);
        rb_exhibition_match.setOnClickListener(this);
        id_activity_main_vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectRadioButton(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {
        fragments.add(new ArticleFragment());
        fragments.add(new ExhibitionMatchFragment());
        radioButtons.add(rb_article);
        radioButtons.add(rb_exhibition_match);

        selectRadioButton(0);
        id_activity_main_vp.setCurrentItem(0);

        if (mAdapter == null) {
            mAdapter = new MainPagerAdapter(SearchExhibitionActivity.this.getSupportFragmentManager(), fragments);
            id_activity_main_vp.setAdapter(mAdapter);

        }

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.rb_article:
                id_activity_main_vp.setCurrentItem(0);
                break;
            case R.id.rb_exhibition_match:
                id_activity_main_vp.setCurrentItem(1);
                break;
            case R.id.cancel:
                SearchExhibitionActivity.this.finish();
                break;
            case R.id.clear_phone:
                edit_search.setText("");
                break;
            default:
                break;

        }
    }

    /**
     * 设置当前选中的radiobutton的文字颜色
     *
     * @param i
     */
    private void selectRadioButton(int i) {
        for (int j = 0; j < 2; j++) {
            if (j == i) {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.sjs_text));
                radioButtons.get(j).setChecked(true);
            } else {
                radioButtons.get(j).setTextColor(getResources().getColor(R.color.home_text));
            }
        }

    }

    private final EditText.OnEditorActionListener editorActionListener =
            new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_GO) {
                        searchGoing();
                    }
                    return true;
                }
            };


    private void searchGoing() {
        String searchContent = edit_search.getText().toString();
        EventBus.getDefault().post(searchContent);

    }
}
